package com.znb.okdown

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.znb.okdown.databinding.ActivityMainBinding
import com.znb.okdown.flutter.Constants
import com.znb.okdown.flutter.FlutterBridge
import com.znb.okdown.flutter.FlutterCacheManager
import com.znb.okdown.flutter.PluginStateManager
import com.znb.okdown.net.SSEClient
import com.znb.okdown.ui.NativeActivity
import com.znb.okdown.ui.adapter.ViewPager2Adapter
import com.znb.okdown.ui.fragment.FltFragment
import com.znb.okdown.ui.fragment.FltViewFragment
import com.znb.okdown.ui.fragment.HomeFragment
import com.znb.okdown.util.StatusBar
import com.znb.okdown.util.ToastUtil
import io.flutter.embedding.android.FlutterFragment
import io.flutter.plugin.common.MethodChannel

class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        StatusBar.fullScreen(this, false)
        StatusBar.setAndroidNativeLightStatusBar(this, false)
        init()
        SSEClient.getInstance().chatOkhttpSSE("中国台湾")
    }

    private fun init() {
        val viewPager2Adapter = ViewPager2Adapter(this, initFragments())
        binding.apply {
            viewPager2.adapter = viewPager2Adapter
            viewPager2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    when (position) {
                        0 -> {
                            bottomNavigationView.selectedItemId = R.id.menu_home
                            setTitle("Home")
                        }

                        1 -> {
                            bottomNavigationView.selectedItemId = R.id.menu_award
                            setTitle("Mine")
                        }

                        2 -> {
                            bottomNavigationView.selectedItemId = R.id.menu_set
                            setTitle("FlutterView")
                        }
                    }
                }
            })

            bottomNavigationView.setOnNavigationItemSelectedListener {
                when (it.itemId) {
                    R.id.menu_home -> viewPager2.currentItem = 0
                    R.id.menu_award -> viewPager2.currentItem = 1
                    R.id.menu_set -> viewPager2.currentItem = 2
                }
                true
            }

            textRight.setOnClickListener {
                FlutterBridge.instance.sendMessage(
                    Constants.METHOD_NAME_AND_TO_FLT,
                    "from Native message",
                    object :
                        MethodChannel.Result {
                        override fun success(result: Any?) {
                            ToastUtil.show("success ${result.toString()}")
                        }

                        override fun error(
                            errorCode: String,
                            errorMessage: String?,
                            errorDetails: Any?
                        ) {
                            ToastUtil.show("error errorCode:$errorCode errorMessage:$errorMessage")
                        }

                        override fun notImplemented() {

                        }

                    })
            }
        }

        PluginStateManager.instance.callback.observe(this) {
            println("MainActivity onMethodCall " + it.arguments.toString())
            when (it.method) {
                Constants.METHOD_NAME_FLT_TO_AND -> {
                    it.argument<String>("event")?.let { event ->
                        ToastUtil.show("goToNative：$event")
                        when (event) {
                            "toNativeAct" -> startActivity(Intent(this, NativeActivity::class.java))
                        }
                    }

                    it.argument<String>("androidView")?.let { event ->
                        ToastUtil.show("goToNative：$event")
                    }
                }
            }
        }
    }

    private fun setTitle(title: String) {
        binding.tvBaseTitle.text = title
    }

    private fun initFragments(): List<Fragment> {
        return arrayListOf<Fragment>(
            HomeFragment.newInstance(),
            FlutterFragment.withCachedEngine(FlutterCacheManager.MINE).build(),
            FltViewFragment.newInstance(),
        )
    }

}
